<?php
/**
 * Created by PhpStorm.
 * User: baidu
 * Date: 17/8/5
 * Time: 下午4:37
 */
namespace app\api\controller\v1;

use app\api\controller\v1\AuthBase;
use think\Controller;
use app\common\lib\exception\ApiException;
use app\common\lib\Aes;
use app\common\lib\IAuth;

/**
 * 客户端auth登录权限基础类库
 * 1.每个接口(需要登录 个人中心  点赞 评论) 都需要去集
 * 2.判定access_user_token是否合法
 * 3.用户信息->>user
 */
class User extends AuthBase {

  public function save(){

  }

  /**
   * [read 获取用户信息]
   * @return [type] [description]
   */
  public function read(){
  	$obj = new Aes(); 
  	 return show(config('code.success'),'ok',$obj->encrypt($this->user),200);
  }


    public function update(){
        $postData = input('param.');
        // validate进行校验
        $data = [];
        if (!empty($postData['image'])) {
            $data['image'] = $postData['image'];
        }
        if (!empty($postData['username'])) {
            $data['username'] = $postData['username'];
        }
        if (!empty($postData['sex'])) {
            $data['sex'] = $postData['sex'];
        }
       if (!empty($postData['signature'])) {
            $data['sex'] = $postData['sex'];
        }
       if (!empty($postData['password'])) {
            //传输过程中需要加密传输
            $data['password'] = IAuth::setPassword($postData['password']);
        }
        if (!$data) {
            return show(config('code.error'),'数据不合法','',404);
        }

        try {
         $id = model('User')->allowField(true)->save($data,['id'=>$this->user->id]);
          if ($id) {
            return show(config('code.success'),'ok',[],202);
          }else{
            return show(config('code.error'),'update falied',[],401);
          }
        } catch (\Exception $e) {
          return show(config('code.error'),$e->getMessage(),[],404);
        }
    }
}